import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useSystemStore = defineStore('system', {
    state: () => ({
        realTimeData: null,
        batteryChartData: [],
        inverterChartData: [],
        powerChartData: [],
        alarmChartData: []
    }),

    getters: {
        systemInfo: (state) => {
            if (!state.realTimeData) return []
            return [{
                    title: '逆变器功率',
                    value: state.realTimeData.inverterPower,
                    unit: 'W',
                    status: state.realTimeData.deviceStatus.inverter
                },
                {
                    title: '电池SOC',
                    value: state.realTimeData.batterySOC,
                    unit: '%',
                    status: state.realTimeData.deviceStatus.battery
                },
                {
                    title: '光伏功率',
                    value: state.realTimeData.solarPower,
                    unit: 'W',
                    status: state.realTimeData.deviceStatus.solar
                },
                {
                    title: '负载功率',
                    value: state.realTimeData.solarPower,
                    unit: 'W',
                    status: state.realTimeData.deviceStatus.solar
                }
            ]
        }
    },

    actions: {
        updateRealTimeData(data) {
            this.realTimeData = data
            this.updateChartData(data)
        },

        updateChartData(data) {
            //   this.batteryChartData = data.batteries
            //   this.inverterChartData = data.charts.power
            //   this.powerChartData = data.charts.power
            // this.alarmChartData=data.charts.alarms

            if (!data) return
            this.batteryChartData = data.batteries || []
                // this.inverterChartData = data.charts ? .power || []
                // this.powerChartData = data.charts ? .power || []
                // this.alarmChartData = data.charts ? .alarms || []
            this.inverterChartData = (data && data.charts && data.charts.power) ? data.charts.power : [];
            this.powerChartData = (data && data.charts && data.charts.power) ? data.charts.power : [];
            this.alarmChartData = (data && data.charts && data.charts.alarms) ? data.charts.alarms : [];

        }
    }


})